package com.example.goodsmanagementcabinet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.goodsmanagementcabinet.R;
import com.example.goodsmanagementcabinet.base.BaseActivity;
import com.example.goodsmanagementcabinet.base.GoodBoxManagerApp_;
import com.example.goodsmanagementcabinet.bean.KeyBean;
import com.example.goodsmanagementcabinet.bean.UserBean;
import com.example.goodsmanagementcabinet.db.DatabaseManager;
import com.example.goodsmanagementcabinet.framents.AcapterFrament;
import com.example.goodsmanagementcabinet.framents.AcapterFraments;
import com.example.goodsmanagementcabinet.otto.OttoMsg;
import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.io.Serializable;
import java.util.List;


@Fullscreen
@EActivity(R.layout.admin_manager_view)
public class AdminManagerView extends BaseActivity implements View.OnClickListener {

    @ViewById
    LinearLayout handOverAndTakeover,btnBrrowKey, btnReturnKey, btnOpenAllDoor, btnKeyManager, btnRecord, btnUserManager, btnTimeManager, btnPassword;
    @ViewById
    ImageView toHome;
    @Extra
    public UserBean user;

    @AfterViews
    public void afterView() {
        handOverAndTakeover.setOnClickListener(this);
        btnBrrowKey.setOnClickListener(this);
        btnReturnKey.setOnClickListener(this);
        btnOpenAllDoor.setOnClickListener(this);
        btnKeyManager.setOnClickListener(this);
        btnRecord.setOnClickListener(this);
        btnUserManager.setOnClickListener(this);
        btnTimeManager.setOnClickListener(this);
        btnPassword.setOnClickListener(this);
        toHome.setOnClickListener(this);
        Intent intent = getIntent();
        user = (UserBean) intent.getSerializableExtra("User");
        updateUI(user);
    }

    @UiThread
    public void updateUI(UserBean user) {
        if (!user.getUserId().equals("admin")) {
            btnOpenAllDoor.setVisibility(View.GONE);
            btnKeyManager.setVisibility(View.GONE);
            btnRecord.setVisibility(View.GONE);
            btnUserManager.setVisibility(View.GONE);
            btnTimeManager.setVisibility(View.GONE);
            btnPassword.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBrrowKey:
                if (user.getUserId().equals("admin")) {
                    List<KeyBean> myKeyList = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryKeyByStatus("1");
                    AcapterFraments a = new AcapterFraments();
                    Bundle arg = new Bundle();
                    arg.putSerializable("KeyList", (Serializable) myKeyList);
                    a.setArguments(arg);
                    a.show(getFragmentManager(), "AcapterFrament");
                } else {
                    List<KeyBean> myKeyList = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryKeyByUserId(user.getUserId(), "1");
                    AcapterFraments a = new AcapterFraments();
                    Bundle arg = new Bundle();
                    arg.putSerializable("KeyList", (Serializable) myKeyList);
                    a.setArguments(arg);
                    a.show(getFragmentManager(), "AcapterFrament");
                }
                break;
            case R.id.btnReturnKey:
                if (user.getUserId().equals("admin")) {
                    List<KeyBean> myKeyList = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryKeyByStatus("2");
                    AcapterFrament a = new AcapterFrament();
                    Bundle arg = new Bundle();
                    arg.putSerializable("KeyList", (Serializable) myKeyList);
                    a.setArguments(arg);
                    a.show(getFragmentManager(), "AcapterFrament");
                } else {
                    List<KeyBean> myKeyList = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryKeyByUserId(user.getUserId(), "2");
                    AcapterFrament a = new AcapterFrament();
                    Bundle arg = new Bundle();
                    arg.putSerializable("KeyList", (Serializable) myKeyList);
                    a.setArguments(arg);
                    a.show(getFragmentManager(), "AcapterFrament");
                }
                break;
            case R.id.btnOpenAllDoor:
                break;
            case R.id.btnKeyManager:
                Intent intent = new Intent(this, AdminGoodManager_.class);
                startActivity(intent);
                break;
            case R.id.btnRecord:
                Intent intents = new Intent(this, AdminGoodManager_.class);
                startActivity(intents);
                break;
            case R.id.btnUserManager:
                Intent userIntent = new Intent(this, UserManagerView_.class);
                startActivity(userIntent);
                break;
            case R.id.btnTimeManager:
                break;
            case R.id.btnPassword:
                break;
            case R.id.toHome:
                acapterDialog();
                break;
            case R.id.handOverAndTakeover:
                Intent hanoverintent;
                hanoverintent = new Intent(AdminManagerView.this, HandOverAndTakeOverActivity_.class);
                hanoverintent.putExtra("user", user);
                startActivity(hanoverintent);
                break;
        }
    }

    @Subscribe
    @Background
    public void handlerOttoMsg(OttoMsg msg) {
        if (TextUtils.equals(OttoMsg.OTTO_TO_HOME, msg.which)) {
            acapterDialog();
        }

    }
}
